home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / CollectionClassesTest.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  6.0 KB  |  251 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1992-93 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Programmer: Kent Sandvik
  5.   Date: 11/7/92
  6.   Revision comments are at the end of this file.
  7.   ---
  8.   The following collection classes are implemented: TLinkedList, TStack, (TQueue, TDeQueue),
  9.   THashTable.
  10.   CollectionClasses.cp contains the collection class member functions. 
  11.   _________________________________________________________________________________________________________ */
  12.  
  13. #ifndef _COLLECTION
  14. #include "CollectionClasses.h"
  15. #endif
  16.  
  17. // FLAGS that will enable/disable tests
  18. //#define TLINKEDLIST
  19. //#define TSTACK
  20. //#define TQUEUE
  21. #define TDEQUE
  22. //#define THASHTABLE
  23.  
  24. void DoSomething(THashEntryPtr p);                // our MapCar test function
  25.  
  26.  
  27. void main(void)
  28. {
  29.     cout << "Start of collection class tests…\n";
  30.  
  31. // _________________________________________________________________________________________________________ //
  32. // TLINKEDLIST TEST
  33.  
  34. #ifdef TLINKEDLIST
  35.     cout << "\nTest1: TLinkedList test…\n";
  36.  
  37.     //     Create a TLinkedList
  38.  
  39.     TLinkedList myList;
  40.  
  41.     //    Append elements to the list
  42.  
  43.     myList.Append(1);
  44.     myList.Append(2);
  45.     myList.Append(3);
  46.     myList.Append(4);
  47.     // Show values in list
  48.     myList.Reset();
  49.     long val;
  50.     while ((val = myList.Next()) != NULL)
  51.         cout << "TLinkedList Entry =  " << val << "\n";
  52.  
  53.     // Show first entry
  54.     cout << "First entry = " << myList.First() << "\n";
  55.  
  56.     // Show last entry
  57.     cout << "Last entry = " << myList.Last() << "\n";
  58.  
  59.     // Try to find entry
  60.     cout << "Trying to find entry 3…\n";
  61.     Boolean OK = myList.Find(3);
  62.     if (OK)
  63.         cout << "We found 3, OK!\n";
  64.     else
  65.         cout << "Ouch, problems, time to debug…";
  66.  
  67.  
  68.     // Remove two entries, 2 and 4
  69.     myList.Remove(2);
  70.     myList.Remove(4);
  71.     cout << "Removed 2 and 4, list looks now like:\n";
  72.     myList.Reset();
  73.     while ((val = myList.Next()) != NULL)
  74.         cout << "TLinkedList Entry =  " << val << "\n";
  75.  
  76. #endif
  77.  
  78. // _________________________________________________________________________________________________________ //
  79. // TSTACK TEST
  80.  
  81. #ifdef TSTACK    
  82.     //    TSTACK TEST PHASE
  83.     cout << "Test2: test TStack:\n";
  84.  
  85.     TStack myStack;
  86.  
  87.     myStack.Push(1 L);
  88.     myStack.Push(2 L);
  89.     myStack.Push(3 L);
  90.     myStack.Push(4 L);
  91.     myStack.Push(5 L);
  92.  
  93.     // test our linked list properties
  94.     cout << "First entry =  " << myStack.First() << "\n";
  95.     cout << "Last entry =  " << myStack.Last() << "\n";
  96.  
  97.     // test our next loop
  98.     myStack.Reset();
  99.     long val;
  100.     while ((val = myStack.Next()) != NULL)
  101.         cout << "Entry =  " << val << "\n";
  102.  
  103.  
  104.     // test Find
  105.     long item1 = 4 L;
  106.     long item2 = 44 L;
  107.     Boolean outcome;
  108.  
  109.     outcome = myStack.Find(item1);
  110.     if (outcome)
  111.         cout << "We have item1 in queue\n";            // should trigger
  112.     else
  113.         cout << "We don't have item1 in queue\n";
  114.  
  115.     outcome = myStack.Find(item2);
  116.     if (outcome)
  117.         cout << "We have item2 in queue\n";
  118.     else
  119.         cout << "We don't have item2 in queue\n";    // should trigger
  120.  
  121.  
  122.     // test out stack properties    
  123.     while (!myStack.IsEmpty())
  124.         cout << "Popping from the stack, value = " << myStack.Pop() << "\n";
  125.  
  126.     cout << "End of TStack test!\n";
  127.  
  128. #endif
  129.  
  130. // _________________________________________________________________________________________________________ //
  131. // TQUEUE TEST
  132.  
  133. #ifdef TQUEUE
  134.     cout << "\nTest3: TQueue test…\n";
  135.  
  136.     // Create TQueue
  137.     TQueue myQueue;
  138.  
  139.     // Place stuff in the queue
  140.     myQueue.Put(10);
  141.     myQueue.Put(20);
  142.     myQueue.Put(30);
  143.     myQueue.Put(40);
  144.  
  145.  
  146.     // Test out .Last()
  147.     cout << "Last, or actually the first pushed entry is = " << myQueue.Last() << "\n";
  148.  
  149.     // get out 2 more values
  150.     cout << " Get =  " << myQueue.Get();
  151.     cout << " Get = " << myQueue.Get();
  152.     cout << "\nI will get out two more values, and the next last value is 30 = " << myQueue.Last() << "\n";
  153.  
  154.  
  155.     // Get Stuff from queue
  156.  
  157.     while (!myQueue.IsEmpty())
  158.         cout << "Get values from the queue, value = " << myQueue.Get() << "\n";
  159.  
  160. #endif
  161.  
  162. // _________________________________________________________________________________________________________ //
  163. // TDEQUE TEST
  164.  
  165. #ifdef TDEQUE
  166.     cout << "\nTest4: TDeque test…\n";
  167.  
  168.     // Create TDeque
  169.     TDeque myDeque;
  170.  
  171.     // Place stuff at end of deque
  172.     myDeque.Push(11);
  173.     myDeque.Push(12);
  174.     myDeque.Push(13);
  175.     myDeque.Push(14);
  176.  
  177.     myDeque.PutAtEnd(21);
  178.     myDeque.PutAtEnd(22);
  179.  
  180.     cout << "Pop (14) = " << myDeque.Pop() << "\n";
  181.     cout << "Pop (13) = " << myDeque.Pop() << "\n";
  182.     cout << "Get (22) = " << myDeque.Get() << "\n";
  183.     
  184.     cout << "The list should now look like: 21 11 12\n";
  185.  
  186.     // empty the deque
  187.     while (!myDeque.IsEmpty())
  188.         cout << "Get values from the dequeue, value = " << myDeque.Get() << "\n";
  189.  
  190. #endif
  191.  
  192.  
  193. // _________________________________________________________________________________________________________ //
  194. // THASHTABLE TEST
  195.  
  196. #ifdef THASHTABLE
  197.     //    THASHTABLE TEST PHASE
  198.     cout << "\nTest6: THashTable test…\n";
  199.  
  200.     // Create Hashtable.    
  201.     THashTable myHashTable;
  202.  
  203.     // Add entries to the Hashtable.    
  204.     myHashTable.Add(4, 100);
  205.     myHashTable.Add(5, 400);
  206.     myHashTable.Add(6, 500);
  207.  
  208.     // Find entry/value in HashTable
  209.  
  210.     long tableValue = myHashTable.Find(4);
  211.     cout << "The value is " << tableValue << ", and should be 100\n";
  212.  
  213.     // remove this entry
  214.     myHashTable.Remove(4);
  215.  
  216.     // now try to find it, and signal we had problems
  217.     TItemtype entry = myHashTable.Find(4);
  218.     if (entry == 0)
  219.         cout << "THashTable.Remove() seems to work fine!\n";
  220.     else
  221.         cout << "Eh, a value, and we just deleted the entry, start debugging THashTable.Remove()!\n";
  222.  
  223.     // MapCar test
  224.     myHashTable.MapCar((MapFun)DoSomething);
  225.  
  226.     cout << "End of THashTable test…\n";
  227. #endif
  228.  
  229.     cout << "End of collection class tests!\n";
  230. }
  231.  
  232.  
  233. // This function is needed for the THashTable.MapCar test
  234. void DoSomething(THashEntryPtr p)
  235. {
  236.     // print out the value inside each THashEntryPtr…
  237.     cout << "Hash Table entry = " << p->fValue << "\n";
  238. }
  239.  
  240.  
  241. // _________________________________________________________________________________________________________ //
  242.  
  243.  
  244. /*    Change History (most recent last):
  245.   No        Init.    Date        Comment
  246.   1            khs        11/7/92        New file
  247.   2            khs        11/28/92    Added TLinkedList
  248.   3            khs        11/29/92    Added TQueue and TDeque
  249.   4            khs        1/14/93        Cleanup
  250. */
  251.